<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");

session_start();
$objMemberInfo = $_SESSION["Member_Info"];
if(time() - $objMemberInfo -> WagerCommonTime <= 1){
	sleep(1);
}
$_SESSION["Member_Info"] -> WagerCommonTime = time();
if($objMemberInfo -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

$_LangPath = "wagercommon";	//語言檔參數，務必放在最前面
include_once(__Member_Path."/logincheck.php");
include_once(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");

$intLID = (int)$_data["LottoID"];
$intGID = (int)$_data["GambleID"];
$intLType = (int)$_arrLotto[$intLID]["LType"];
$intBT = (int)$_data["Bet_Type"];
$arrLotto = $_arrLotto[$intLID];
$arrChoiceNumber = $_data["ChoiceNumber"];
$strChoiceNumber = "";
$intOddsID = 0;
$flOdds = 0;

$arrNum = array();
$intNum = 0;
if(is_array($arrChoiceNumber)){
	foreach($arrChoiceNumber[3] as $intCent){	//百位
		foreach($arrChoiceNumber[2] as $intDec){	//十位
			foreach($arrChoiceNumber[1] as $intUnit){	//個位
				$intNum = $intCent*100+$intDec*10+$intUnit*1;
				if($intBT == 13){
					if($intNum < 100){
						if(floor($intNum / 10) > ($intNum % 10))continue;
					}else{
						if(floor($intNum / 10) > ($intNum% 100))continue;
						if(floor(($intNum % 100) / 10) > ($intNum % 10))continue;
					}
				}
				$arrNum[] = $intNum;
			}
		}
	}
}

if(sizeof($arrNum) == 0){
	$Err_Message = "No Wager Number Data!!";
	include("wager_error.php");
}

include_once(__Common_Path."/betparam/BetParam_".$intLID.".inc.php");
include_once(__Common_Path."/limit/LottoLimit_".$intLID.".inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/RealBet.lib.php");
include_once(__Common_Path."/gambletype/GambleType".$objMemberInfo -> GambleType."_".$intLID.".inc.php");//加進分盤資料

if(__isConnectToOtherWeb == 1){
	$flMemERate = (empty($objMemberInfo -> ExRate))?1:$objMemberInfo -> ExRate;
}else{
	$flMemERate = (empty($_ECRate[$objMemberInfo -> ERID]["Rate"]))?1:$_ECRate[$objMemberInfo -> ERID]["Rate"];
}
$intApointNum = $_arrGLineNumToApointNum[$intLID][$intBT][$intNum];
if($intApointNum == null)$intApointNum = 0;

if(!is_array($_arrBetParam[$intBT])){
	$Err_Message = "No Wager Relation Data!!";
	include("wager_error.php");
}

include_once(__Common_Path."/CommToOddsRate_".$intLID.".inc.php");
include_once(__Modules_Path."/Commission.lib.php");
//找出 使用者 的賠率兌換，傳回陣列
$arrUserOPlus = getUserCommToOdds($objMemberInfo -> ID, $intLID, array($intBT));
if($arrUserOPlus === false)ErrMsgOutputHTML("Error", "", 1);
$flOddsPlus = $arrUserOPlus[$intBT][$intApointNum][11] * $_arrChRate[$intBT][$intApointNum][$objMemberInfo -> GambleType+10];

$intMaxUsedNumGold = 0;	//選擇號碼中，已下注最大金額
$flMaxOdds = 0; //選擇號碼中，最大賠率
$arrWagerCon = array();
$intTotalWNo = 0;
//取得最新號碼賠率資料
foreach($arrNum as $intNum){
	$arrGameDate = getWagerArrayData($intLID, $intLType, $intGID, $intBT, $intNum, $intOddsID, $flOdds, $strChoiceNumber);
	if($arrGameDate["isError"]){
		$Err_Message = $_strWagerCommon[$arrGameDate["Err_Message"]];
		include("wager_error.php");
	}
	$flOdds = floatval($arrGameDate["Odds"]) + $_arrGambleType[$intBT][$intApointNum] + $flOddsPlus;
	$arrWagerCon[$intNum] = getWagerConditions($intLType, $intBT, $intNum, $flOdds, $arrGameDate["SerialNo"]);
	if($intTotalWNo != 0)$arrWagerCon[$intNum] = preg_replace("/.+<br \/>/", "", $arrWagerCon[$intNum]);
	$intTotalWNo++;

	$arrFrmWagerCon[$intNum] = $intNum."#".$arrGameDate["OddsID"]."#00000#".$intBT;
	$intTmp = $_arrMemStake[$intBT][$intGID][$intNum];
	if($intMaxUsedNumGold < $intTmp)$intMaxUsedNumGold = $intTmp;
	if($flMaxOdds < $flOdds)$flMaxOdds = $flOdds;
}
$strWagerConditions = WagerFormTrans($intLID, $intBT, join("<br />", $arrWagerCon));
//print_r($arrChoiceNumber);
//是否上層停押
/*$strParentID = $objMemberInfo -> SubID.",".$objMemberInfo -> MainID.",".$objMemberInfo -> StockID;
if(!$strTop = GetOneStopWagerBet($intGID, $intBT, $intNum, $strParentID)){
	$Err_Message = $GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}

if($strTop == "Stop"){
	$Err_Message = $_strWagerCommon["StopWager"];
	include("wager_error.php");
}*/

include_once(__Root_Path."/tmp/credit-".$objMemberInfo -> ID.".php");


$fp = @file(__Membertemplate_Path."/number92_qwager.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);
$fp = str_replace("{sid}", $_data["sid"], $fp);
$fp = str_replace("{ShowAccount}", $objMemberInfo -> Account, $fp);
$fp = str_replace("{ShowCredit}", $objMemberInfo -> CreditLine, $fp);
$fp = str_replace("{BetTypeName}", $_arrBet_Type[$arrLotto["LType"]][$intBT], $fp);
$fp = str_replace("{LottoName}", $arrLotto["Lotto_".$_intLang], $fp);
$fp = str_replace("{GambleIDValue}", $intGID, $fp);
$fp = str_replace("{Bet_TypeValue}", $intBT, $fp);
$fp = str_replace("{LottoIDValue}", $intLID, $fp);
$fp = str_replace("{NumberValue}", $arrGameDate["Number"], $fp);
$fp = str_replace("{UserIDValue}", $objMemberInfo -> ID, $fp);
$fp = str_replace("{WagerConditions}", $strWagerConditions, $fp);

$intMinStakeValue = 9999999;
$intChkMaxPayOff = 9999999;
$intCom_BetLimit = 9999999;
foreach($_arrBetParam[$intBT] as $intAN => $arrBP){
	if(floor($arrBP["MinStake"] / $flMemERate) < $intMinStakeValue)$intMinStakeValue = floor($arrBP["MinStake"] / $flMemERate);
	if(floor($arrBP["MaxPayOff"] / $flMemERate) < $intChkMaxPayOff)$intChkMaxPayOff = floor($arrBP["MaxPayOff"] / $flMemERate);
	if($arrBP["Com_BetLimit"] < $intCom_BetLimit)$intCom_BetLimit = $arrBP["Com_BetLimit"];
}
$fp = str_replace("{MinStake}", (string)$intMinStakeValue, $fp);	//最小下注金額
$fp = str_replace("{MaxPayOff}", $intChkMaxPayOff, $fp);	//最高派彩
$fp = str_replace("{BetLimit}", $objMemberInfo -> arrBetLimit[$intLID][$intBT], $fp);	//單注限額
$fp = str_replace("{EventLimit}", $objMemberInfo -> arrEventLimit[$intLID][$intBT], $fp);	//單號限額
$fp = str_replace("{ShowStakeMoney}", "", $fp);
$fp = str_replace("{ShowWagerContent}", join("@", $arrFrmWagerCon), $fp);
$fp = str_replace("{MaxOdds}", $flMaxOdds, $fp);
$fp = str_replace("{intTotalWNo}", $intTotalWNo, $fp);

//當會員單場限額，大於此樂透單場限額
$chkEventLimit = $objMemberInfo -> arrEventLimit[$intLID][$arrGameDate["Bet_Type"]];
$arrOtherMessage = array();
$_arrLottoLimit[$arrGameDate["Bet_Type"]][0] = floor($_arrLottoLimit[$arrGameDate["Bet_Type"]][0] / $flMemERate);
if($chkEventLimit > $_arrLottoLimit[$arrGameDate["Bet_Type"]][0]){
	$chkEventLimit = $_arrLottoLimit[$arrGameDate["Bet_Type"]][0];
	$arrOtherMessage[0] = $_strWagerCommon["MaxEventLimit"].$chkEventLimit;
}
//當會員單注限額，大於此樂透單注限額
$chkBetLimit = $objMemberInfo -> arrBetLimit[$intLID][$arrGameDate["Bet_Type"]];
$_arrLottoLimit[$arrGameDate["Bet_Type"]][1] = floor($_arrLottoLimit[$arrGameDate["Bet_Type"]][1] / $flMemERate);
if($chkBetLimit > $_arrLottoLimit[$arrGameDate["Bet_Type"]][1]){
	$chkBetLimit = $_arrLottoLimit[$arrGameDate["Bet_Type"]][1];
	$arrOtherMessage[1] = $_strWagerCommon["MaxBetLimit"].$chkBetLimit;
}
//當會員單注限額，大於公司預收單注限額
$CompBetLimit = floor($intCom_BetLimit / ($objMemberInfo -> Layer6Scale*$flMemERate));
if($chkBetLimit > $CompBetLimit){
	$chkBetLimit = $CompBetLimit;
	$arrOtherMessage[1] = $_strWagerCommon["MaxBetLimit"].$chkBetLimit;
}
if(sizeof($arrOtherMessage)){
	$fp = str_replace("{OtherMessage}", join("<br>", $arrOtherMessage), $fp);
}else{
	$fp = str_replace("{OtherMessage}", "", $fp);
}
$fp = str_replace("{chkBetLimit}", $chkBetLimit, $fp);
$fp = str_replace("{chkEventLimit}", $chkEventLimit, $fp);

$fp = str_replace("{UseLimitValue}",$intMaxUsedNumGold, $fp);	//已使用信用額度
$intTmp = $_arrMemStake["_SUM"];
if(empty($intTmp))$intTmp = 0;
$fp = str_replace("{UseCreditValue}",$intTmp, $fp);	//已使用信用額度

$arrCheckKey = split(" ", microtime());
$fp = str_replace("{CheckKeyValue1}", $arrCheckKey[0], $fp);
$fp = str_replace("{CheckKeyValue2}", $arrCheckKey[1], $fp);

if($arrGameDate["isMessage"]){
	$fp = str_replace("{WagerMessage}", $_strWagerCommon[$arrGameDate["Message"]], $fp);
}else{
	$fp = str_replace("{WagerMessage}", "", $fp);
}

while(list($k, $v) = each($_strWagerCommon)){
	$fp = str_replace("{".$k."}", $v, $fp);
}

//輸出
WebOutputHTML($fp, 1);
?>